// Copyright 2022 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.mediapipe.tasks.audio.audioembedder;

import com.google.auto.value.AutoValue;
import com.google.mediapipe.tasks.components.containers.EmbeddingResult;
import com.google.mediapipe.tasks.components.containers.proto.EmbeddingsProto;
import com.google.mediapipe.tasks.core.TaskResult;
import java.util.ArrayList;
import java.util.List;

/** Represents the embedding results generated by {@link AudioEmbedder}. */
@AutoValue
public abstract class AudioEmbedderResult implements TaskResult {

  /**
   * Creates an {@link AudioEmbedderResult} instance from a list of {@link
   * EmbeddingsProto.EmbeddingResult} protobuf messages.
   *
   * @param protoList a list of {@link EmbeddingsProto.EmbeddingResult} protobuf message to convert.
   * @param timestampMs a timestamp for this result.
   */
  static AudioEmbedderResult createFromProtoList(
      List<EmbeddingsProto.EmbeddingResult> protoList, long timestampMs) {
    List<EmbeddingResult> embeddingResultList = new ArrayList<>();
    for (EmbeddingsProto.EmbeddingResult proto : protoList) {
      embeddingResultList.add(EmbeddingResult.createFromProto(proto));
    }
    return new AutoValue_AudioEmbedderResult(embeddingResultList, timestampMs);
  }

  /**
   * Creates an {@link AudioEmbedderResult} instance from a {@link EmbeddingsProto.EmbeddingResult}
   * protobuf message.
   *
   * @param proto the {@link EmbeddingsProto.EmbeddingResult} protobuf message to convert.
   * @param timestampMs a timestamp for this result.
   */
  static AudioEmbedderResult createFromProto(
      EmbeddingsProto.EmbeddingResult proto, long timestampMs) {
    List<EmbeddingResult> embeddingResultList = new ArrayList<>();
    embeddingResultList.add(EmbeddingResult.createFromProto(proto));
    return new AutoValue_AudioEmbedderResult(embeddingResultList, timestampMs);
  }

  /**
   * A list of of timpstamped {@link EmbeddingResult} objects, each contains one set of results per
   * embedder head.
   *
   * <p>In the "audio stream" mode, the list only contains one element, representing the embedding
   * result of the audio block that starts at {@link EmbeddingResult.timestampMs} in the audio
   * stream. Otherwise, in the "audio clips" mode, the list may include multiple {@link
   * EmbeddingResult} objects, each contains the embedding of an interval of the entire audio clip
   * that starts at {@link EmbeddingResult.timestampMs}.
   */
  public abstract List<EmbeddingResult> embeddingResults();

  @Override
  public abstract long timestampMs();
}
